local Msg = require("core.Msg")
local FcmLogic = require("fcm.FcmLogic")
local FcmDefine = require("fcm.Define")

local ERR_ALREADY_REGISTERED = 1 -- 已经防沉迷注册了
local ERR_INVALID_IDCARD = 2     -- 无效的身份证号码
local ERR_AGE = 3				 -- 年龄太小

function CG_REGISTER_FCM(human, msg)
	if FcmDefine.IsFcmTurnedOn ~= true then  return end
	
    local idCard = msg.idCard
	--服务端的验证防沉迷流程
	local gcRegisterFcm = Msg.gc.GC_REGISTER_FCM
    if FcmLogic.isFcmRegistered(human) then
        gcRegisterFcm.retCode = ERR_ALREADY_REGISTERED
        gcRegisterFcm.errMsg = "当前帐号已通过防沉迷注册"
        Msg.send(gcRegisterFcm, human.fd)
        return
    end

    local checkRet = FcmLogic.checkIdCard(idCard)
    if checkRet ~= 0 then
        if checkRet == 2 then 
            gcRegisterFcm.retCode = ERR_AGE 
            gcRegisterFcm.errMsg = "输入身份证年龄小于18岁，不能通过防沉迷"
        else
            gcRegisterFcm.retCode = ERR_INVALID_IDCARD
            gcRegisterFcm.errMsg = "输入身份证格式错误"
        end
        Msg.send(gcRegisterFcm, human.fd)
        return
    end

    FcmLogic.setFcmRegistered(human)

    gcRegisterFcm.retCode = 0 
    gcRegisterFcm.errMsg = "通过防沉迷"
    Msg.send(gcRegisterFcm, human.fd)
end